﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;

namespace Nop.Web.Framework.UI
{
    public static class UnorderedListExtensions
    {
        public static IHtmlString UnorderedList<T>(this HtmlHelper helper, IEnumerable<T> items, int columns,
            Func<T, HelperResult> template) 
            where T : class
        {
            if (items == null)
                return new HtmlString("");

            var sb = new StringBuilder();
            sb.Append("<ul ");
            sb.Append("class=");
            sb.Append("items");
            sb.Append(">");

            var itemcount = 0;
            foreach (T item in items)
            {
                var itemcolumn = itemcount % columns + 1;
                sb.Append("<li class='column-" + itemcolumn + "'>");
                sb.Append(template(item).ToHtmlString());
                sb.Append("</li>");
                itemcount++;
            }

            sb.Append("</ul>");

            return new HtmlString(sb.ToString());
        }
    }
}
